<template>
  <div class="employees-container">
    <div class="app-container">
      <Pagetools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button
            type="warning"
            size="small"
            @click.native="$router.push('/import')"
            >excel导入</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click.native="exportExcel"
            :loading="downloadLoading"
            >excel导出</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click.native="showDialog = true"
            >新增员工</el-button
          >
        </template>
      </Pagetools>

      <el-card style="margin-top: 10px">
        <el-table
          border
          :data="lists"
          :default-sort="{ prop: 'workNumber', order: 'descending' }"
        >
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" width="110">
            <template #default="scope">
              <el-image
                :src="scope.row.staffPhoto"
                class="avatar"
                @click="createQr(scope.row)"
              >
                <!-- 具名插槽新语法  图片加载出错的时候自动显示 -->
                <template #error>
                  <img
                    src="@/assets/images/common/bigUserHeader.png"
                    style="width: 80px"
                  />
                </template>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{
                scope.row.formOfEmployment == 1 ? '正式' : '非正式'
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable>
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.timeOfEntry }}</span>
            </template>
          </el-table-column>
          <el-table-column label="账户状态">
            <template>
              <!-- <el-switch
                v-model="value"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch> -->
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click.native="push(scope.row.id)"
                >查看</el-button
              >
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button
                type="text"
                size="small"
                @click.native="edituser(scope.row.id)"
                >角色</el-button
              >
              <el-button
                type="text"
                size="small"
                @click.native="delEmployees(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            layout="prev, pager, next"
            @current-change="currentChange"
            :total="total"
            :page-size="10"
          />
        </div>
      </el-card>
    </div>
    <Addemployee
      :showDialog="showDialog"
      @close="closeDialog"
      @update="update"
    ></Addemployee>
    <!-- 分享展示, 预览的二维码的弹层 -->
    <el-dialog
      title="二维码"
      :visible="showCodeDialog"
      @close="showCodeDialog = false"
    >
      <!-- 二维码 -->
      <el-row type="flex" justify="center">
        <canvas ref="myCanvas" />
      </el-row>
    </el-dialog>
    <AssignRole
      :showRoleDialog="showRoleDialog"
      @close="close"
      :id="id"
      ref="assign"
    ></AssignRole>
  </div>
</template>

<script>
import QrCode from 'qrcode' // 生成图片二维码
import Addemployee from './components/add-employee.vue'
import dayjs from 'dayjs'
import { getAllUser, delEmployee } from '@/api/employess'
import { getExportData } from '@/components/excelData'
import { getUserDetailById } from '@/api/user'
import AssignRole from '@/views/employees/components/assign-role'
export default {
  // 组件名称
  name: 'employees',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 局部注册的组件
  components: { Addemployee, AssignRole },
  // 组件状态值
  data () {
    return {
      id: '',
      showRoleDialog: false,
      showCodeDialog: false,
      downloadLoading: false,
      showDialog: false,
      // value: true,
      page: {
        page: 1,
        size: 10,
      },
      total: 0,
      lists: [],
      roleidlists: []
    }
  },
  created () {
    // 获取全部元方法
    this.getAllUsers()
  },
  methods: {
    createQr (data) {
      if (data.staffPhoto) {
        this.showCodeDialog = true
        this.$nextTick(() => {
          // Canvas画布dom元素,用来装载图片的
          QrCode.toCanvas(this.$refs.myCanvas, data.staffPhoto)  // 调用生成二维码的QrCode的toCanvas方法  参数是(dom元素,图片url地址)
        })
      }
    },
    push (id) {
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    },
    // 获取全部员工方法
    getAllUsers () {
      getAllUser(this.page).then(res => {
        this.total = res.data.total
        this.lists = res.data.rows
      })
    },
    // 删除员工功能
    delEmployees (id) {
      this.$confirm('确定删除员工吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delEmployee(id).then(res => {
          this.$message.success('删除成功')
          this.lists.length === 1 && this.page.page > 1 && this.page.page--
          this.getAllUsers()
        })
      }).catch(() => { });

    },
    // 分页器
    currentChange (page) {
      this.page.page = page
      this.getAllUsers()
    },
    // 日期转换方法
    formatDate (value, str = 'YYYY-MM-DD') {
      return dayjs(value).format(str)
    },
    // 关闭弹出框
    closeDialog () {
      this.showDialog = false
    },
    // 通知父组件更新页面
    update () {
      this.getAllUsers()
    },
    // 导出excel表格的方法
    exportExcel () {
      import('@/components/export-excel/Export2Excel').then(async excel => {
        this.downloadLoading = true
        const headers = {
          '手机号': 'mobile',
          '姓名': 'username',
          '入职日期': 'timeOfEntry',
          '工号': 'workNumber',
          '转正日期': 'correctionTime',
          '部门': 'departmentName'
        }
        let list = await getAllUser(this.page)
        const { data, header } = getExportData(list.data.rows, headers)
        // excel表示导入的模块对象
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
        this.downloadLoading = false
      })
    },
    // 子组件让父组件关闭角色弹出框
    close (data) {
      this.showRoleDialog = data
    },
    edituser (id) {
      this.id = id
      getUserDetailById(id).then(res => {
        if (res.data.roleIds !== null) {
          this.$refs.assign.idlist = res.data.roleIds
        }
        this.showRoleDialog = true
      })
    }
  }
}
</script>
<style scoped>
</style>

